package top.huhuiyu.myforum.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import top.huhuiyu.myforum.entity.TbUser;
import top.huhuiyu.myforum.model.TbUserModel;
import top.huhuiyu.myforum.dao.TbUserDAO;
import top.huhuiyu.myforum.util.JsonMessage;

/**
 * TbUser的控制器类
 */
@RestController
@RequestMapping("/tbUser")
public class TbUserController {
  
  @Autowired
  private TbUserDAO tbUserDAO;
  
  @RequestMapping("/save")
  public JsonMessage save(TbUserModel tbUserModel) throws Exception {
    // 测试模板 /tbUser/save {"tbUser.username":"1","tbUser.password":"2","tbUser.nickname":"3","tbUser.isEnable":"4","tbUser.created":"5"}
    tbUserDAO.save(tbUserModel.getTbUser());
    return JsonMessage.getSuccessMessage("数据保存成功");
  }
  
  @RequestMapping("/update")
  public JsonMessage update(TbUserModel tbUserModel) throws Exception {
    // 测试模板 /tbUser/update {"tbUser.uid":"1","tbUser.username":"2","tbUser.password":"3","tbUser.nickname":"4","tbUser.isEnable":"5","tbUser.created":"6"}
    tbUserDAO.update(tbUserModel.getTbUser());
    return JsonMessage.getSuccessMessage("数据修改成功");
  }
  
  @RequestMapping("/delete")
  public JsonMessage delete(TbUserModel tbUserModel) throws Exception {
    // 测试模板 /tbUser/delete {"tbUser.uid":"1"}
    tbUserDAO.delete(tbUserModel.getTbUser());
    return JsonMessage.getSuccessMessage("数据删除成功");
  }
  
  @RequestMapping("/queryByKey")
  public JsonMessage queryByKey(TbUserModel tbUserModel) throws Exception {
    // 测试模板 /tbUser/queryByKey {"tbUser.uid":"1"}
    TbUser tbUser = tbUserDAO.queryByKey(tbUserModel.getTbUser());
    JsonMessage message = JsonMessage.getSuccessMessage("数据查询完成");
    message.putData("tbUser",tbUser);
    return message;
  }
  
  @RequestMapping("/queryAll")
  public JsonMessage queryAll(TbUserModel tbUserModel) throws Exception {
    // 测试模板 /tbUser/queryAll {}
    PageHelper.startPage(tbUserModel.getPage().getPageNumber(), tbUserModel.getPage().getPageSize());
    Page<TbUser> list = (Page<TbUser>) tbUserDAO.queryAll();
    tbUserModel.getPage().setPageInfo(list);
    JsonMessage message = JsonMessage.getSuccessMessage("查询成功！");
    message.putData("list", list);
    message.putData("page", tbUserModel.getPage());
    return message;
  }
  
}
